#include "sdk/message/Common.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "WeChatLoginAndroid.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include "WeChatLogin.h"

USING_NS_CC;

extern "C"
{
	const char* ANDROID_PACKAGE = "org/cocos2dx/cpp/AppActivity";
	
	bool isInstanlledAndroid()
	{
		JniMethodInfo info;
		bool isHave = JniHelper::getStaticMethodInfo(info, ANDROID_PACKAGE, "isInstall", "()Z");
		if(isHave){
			bool res = info.env->CallStaticBooleanMethod(info.classID, info.methodID);
			info.env->DeleteLocalRef(info.classID);
			return res;
		}
		return false;
	}

	void authAndroid()
	{
		JniMethodInfo info;
		bool isHave = JniHelper::getStaticMethodInfo(info, ANDROID_PACKAGE, "weixinLogin", "()V");
		if(isHave){
			info.env->CallStaticVoidMethod(info.classID, info.methodID);
			info.env->DeleteLocalRef(info.classID);
		}
	}

	bool checkCanAuthAndroid()
	{
		
	}

	void codeResponseAndroid(const char* code,const char* appId,const char* appSecret)
	{
		WH_LogErr("WeChatLoginAndroid", U2F("==code,==code==appid==appsecret").c_str());
		WH_LogErr("WeChatLoginAndroid", StringUtils::format("code:%s  appid:%s  appsecret:%s", code, appId, appSecret).c_str());
		WeChatLogin::getInstance()->setCode(code);
		WeChatLogin::getInstance()->setAppId(appId);
		WeChatLogin::getInstance()->setSecret(appSecret);

		WeChatLogin::getInstance()->requestAccessToken();
	}

	const char* getAppSecret()
	{
		JniMethodInfo info;
		bool isHave = JniHelper::getStaticMethodInfo(info, ANDROID_PACKAGE, "getAppSecret", "()Ljava/lang/String;");
		if(isHave)
		{
			jstring jstr = (jstring)info.env->CallStaticObjectMethod(info.classID, info.methodID);
			std::string res = JniHelper::jstring2string(jstr);
			info.env->DeleteLocalRef(jstr);
			info.env->DeleteLocalRef(info.classID);
			return res.c_str();
		}
		return "";
	}

	const char* getAppID()
	{
		JniMethodInfo info;
		bool isHave = JniHelper::getStaticMethodInfo(info, ANDROID_PACKAGE, "getAppID", "()Ljava/lang/String;");
		if(isHave)
		{
			jstring jstr = (jstring)info.env->CallStaticObjectMethod(info.classID,info.methodID);
			std::string res = JniHelper::jstring2string(jstr);
			info.env->DeleteLocalRef(jstr);
			info.env->DeleteLocalRef(info.classID);
			return res.c_str();
		}
		return "";
	}

};

#endif
